<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>

</body>
<script>
    /* setTimeout(()=>{
        console.log(11111);
    },2000)

    var xhr = new XMLHttpRequest();
    xhr.open('GET','/api/user',true);
    xhr.onreadystatechange = function(){
        console.log(xhr.response);
    }
    xhr.send(); */

    // 状态
    /* var p = new Promise(function (resolve, reject) {
        setTimeout(() => {
            console.log("异步操作:2s");
            resolve(true);  // {<fulfilled>:true}
            console.log("28", p);
        }, 2000)
    })
    console.log("31", p);  // {<pending>:undefined}

    // 指定Promise实例状态 变为fulfilled或rejected时执行的回调函数
    p.then(() => {
        console.log("fulfilled");
    }).catch(() => {
        console.log("rejected");
    }) */


    // 链式
    var p = new Promise(function (resolve, reject) {
        setTimeout(() => {
           resolve(1)
        }, 2000)
    })
    console.log("31", p);  // {<pending>:undefined}

    // 指定Promise实例状态 变为fulfilled或rejected时执行的回调函数
    // Promise实例 可以同时绑定多个then()方法, 每个then()方法返回一个新的Promise实例 => 后面的then可以接收前面回调函数返回的结果
    /* p.then((res) => {
        console.log("fulfilled",res);
        return 2;
    }).then((res)=>{
        console.log("fulfilled",res);  // 2
        throw "333333333333"
        return 3; 
    }).then((res)=>{
        console.log("fulfilled",res);
    }).catch((err) => {
        console.log("rejected",err);
    }) */


    /* axios().then(responseOption => responseOption.data).then(data=>{

    }) */



    p.then((res) => {
        console.log("fulfilled",res);
        return Promise.resolve(2);
    }).then((res)=>{
        console.log("fulfilled",res);  // 2
        return new Promise(function(resolve,reject){  // Promise实例<pending>  => 3sfulfilled
            setTimeout(()=>{
                resolve(3)
            },3000)
        }); 
    }).then((res)=>{
        console.log("fulfilled",res);
    }).catch((err) => {
        console.log("rejected",err);
    })

    /*  
        this.$store.dispatch("addA_Async").then(()=>{
            return this.$store.dispatch("addA_Async");
        }).then(()=>{

        })

        async function fn(){
           let data1  =  await this.$store.dispatch("addA_Async");
           let data2  =  await this.$store.dispatch("addB_Async");
        }
    
    */


</script>

</html>